/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.x3.purchasing.produk;

import com.x3.purchasing.common.ClassApplicationModule;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.zkoss.image.AImage;
import org.zkoss.image.Image;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Toolbarbutton;

/**
 *
 * @author kris
 */
public class ProdukVendorWnd extends ClassApplicationModule {

    private ProdukDAO produkDAO;
    private Listbox listboxProduk;

    public void onCreate() throws Exception {
        produkDAO = new ProdukDAOImpl();
        listboxProduk = (Listbox) getFellow("listboxProduk");
        this.loadProduk();
    }

    private void loadProduk() throws Exception {
        listboxProduk.getItems().clear();
        List<Produk> produks = new ArrayList<Produk>();
        produks = produkDAO.gets();
        int index = 1;
        for (final Produk produk : produks) {
            Listitem listitem = new Listitem();
            listitem.appendChild(new Listcell(index + ""));
            Listcell cellImage = new Listcell();
            if (produk.getFileGambar() != null) {
                if (produk.getFileGambar().available() > 0) {
                    Image image = new AImage("image", produk.getFileGambar());
                    org.zkoss.zul.Image img = new org.zkoss.zul.Image();
                    img.setContent(image);
                    img.setHeight("120px");
                    img.setWidth("150px");
                    cellImage.appendChild(img);
                }
            } else {
                Label lblError = new Label("Gambar tidak tersedia");
                lblError.setStyle("color:red;");
                cellImage.appendChild(lblError);
            }
            listitem.appendChild(new Listcell(produk.getKode()));
            listitem.appendChild(new Listcell(produk.getNama()));
            listitem.appendChild(new Listcell("Rp. " + new DecimalFormat("#0,000.00").format(produk.getHargaSatuan())));
            listitem.appendChild(new Listcell(produk.getVendor().getNama()));
            listitem.appendChild(cellImage);
            Toolbarbutton btnDelete = new Toolbarbutton();
            btnDelete.setId("btnDelete" + produk.getId());
            btnDelete.setTooltiptext("Klik untuk menghapus data");
            btnDelete.setImage("/img/delete.png");
            btnDelete.addEventListener("onClick", new EventListener() {

                public void onEvent(Event event) throws Exception {
                    delete(produk);
                }
            });
            Listcell cellAksi = new Listcell();
            cellAksi.appendChild(btnDelete);
            listitem.appendChild(cellAksi);
            listboxProduk.appendChild(listitem);
            index++;
        }
    }

    private void delete(Produk produk) throws InterruptedException {
        try {
            produkDAO.delete(produk.getId());
            Messagebox.show("Data sudah dihapus");
            this.loadProduk();
        } catch (Exception ex) {
            Messagebox.show("Gagal menghapus data");
            Logger.getLogger(ProdukVendorWnd.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
